package com.ruoyi.exam.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.exam.vo.ExamQuestionOptionVo;
import com.ruoyi.exam.service.IExamQuestionOptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 题目选项Controller
 *
 * @author Tellsea
 * @date 2025-05-30
 */
@Api(tags = "题目选项Controller")
@RestController
@RequestMapping("/exam/questionOption")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamQuestionOptionController extends BaseController {

    private final IExamQuestionOptionService examQuestionOptionService;

    @ApiOperation("查询题目选项列表")
    @PreAuthorize("@ss.hasPermi('exam:questionOption:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamQuestionOptionVo> list(ExamQuestionOptionVo entity) {
        return examQuestionOptionService.queryList(entity);
    }

    @ApiOperation("查询题目选项所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamQuestionOptionVo entity) {
        return AjaxResult.success("查询成功", examQuestionOptionService.queryAll(entity));
    }

    @ApiOperation("导出题目选项列表")
    @PreAuthorize("@ss.hasPermi('exam:questionOption:export')")
    @Log(title = "题目选项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamQuestionOptionVo entity) {
        List<ExamQuestionOptionVo> list = examQuestionOptionService.queryAll(entity);
        ExcelUtil<ExamQuestionOptionVo> util = new ExcelUtil<>(ExamQuestionOptionVo.class);
        util.exportExcel(response, list, "题目选项数据");
    }

    @ApiOperation("获取题目选项详细信息")
    @PreAuthorize("@ss.hasPermi('exam:questionOption:query')")
    @GetMapping(value = "/getInfo/{optionId}")
    public AjaxResult getInfo(@PathVariable("optionId") Long optionId) {
        return AjaxResult.success("查询成功", examQuestionOptionService.queryById(optionId));
    }

    @ApiOperation("新增题目选项")
    @PreAuthorize("@ss.hasPermi('exam:questionOption:add')")
    @Log(title = "题目选项", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamQuestionOptionVo entity) {
        return toAjax(examQuestionOptionService.save(entity));
    }

    @ApiOperation("修改题目选项")
    @PreAuthorize("@ss.hasPermi('exam:questionOption:edit')")
    @Log(title = "题目选项", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamQuestionOptionVo entity) {
        return toAjax(examQuestionOptionService.updateById(entity));
    }

    @ApiOperation("删除题目选项")
    @PreAuthorize("@ss.hasPermi('exam:questionOption:remove')")
    @Log(title = "题目选项", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{optionIds}")
    public AjaxResult remove(@PathVariable Long[] optionIds) {
        return toAjax(examQuestionOptionService.removeByIds(Arrays.asList(optionIds)) ? 1 : 0);
    }
}
